/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.cqq.statute;

import com.yunhoo.cqq.impl.IbatisStatute;
import com.yunhoo.cqq.model.LawTypeData;
import com.yunhoo.cqq.model.Statute;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.NavigationTool;
import com.yunhoo.sys.util.common.ToolKit;
import com.yunhoo.sys.util.tree.TreeUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.richfaces.event.TreeSelectionChangeEvent;
/**
 *
 * @author Administrator
 */
@ManagedBean(name = "statute")
@SessionScoped
public class StatuteBean {

    private Statute statute = new Statute();//公告类
    private LawTypeData type = new LawTypeData();//公告类型
    private boolean imp = false;//公告重要性
    private List<Statute> statutelist = new ArrayList<Statute>();//table列表数据list
    private List<Statute> selectlist = new ArrayList<Statute>();//被选中判断中间list
    private UIData data = null;
    private boolean all = false;//是否全选
    private boolean addOrUpdate = true;//判断是否是新增
    private List<Statute> top3list = new ArrayList<Statute>();//最新3条公告
    private Statute topone = new Statute();//最近一个公告对象
    private List<Statute> top5list = new ArrayList<Statute>();//最新5个公告
    private List<Statute> leavinglist = new ArrayList<Statute>();//更多公告

    public StatuteBean() {
        statute = new Statute();
        statute.setAuthor(LoginParam.getPer().getCpsn_number());
        statute.setCreatedate(new Date());
        statute.setLastchangedate(new Date());
        type = new LawTypeData();
        statutelist = new ArrayList<Statute>();
        selectlist = new ArrayList<Statute>();
    }

    public void editor() {
//        System.out.println(statute.getType());
        if (statute.getType() == null) {
            
            ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "请先选择类别");
        } else {
            NavigationTool.getSelectedUrl("/page/statute/fckeditor.xhtml");
        }
    }

    public void queryTitle() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String id = request.getParameter("qtitle");
        statute = (Statute) IbatisStatute.getStatuteById(id);
        NavigationTool.getSelectedUrl("/page/statute/readstatute.xhtml");
    }

    public void qid() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String id = request.getParameter("qid");
        statute = (Statute) IbatisStatute.getStatuteById(id);
        NavigationTool.getSelectedUrl("/page/statute/readstatute.xhtml");
    }

    public void qmore() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String id = request.getParameter("qmore");
        statute = (Statute) IbatisStatute.getStatuteById(id);
        NavigationTool.getSelectedUrl("/page/statute/readstatute.xhtml");
    }

    public void readback() {
        NavigationTool.getSelectedUrl("/page/statute/foreground.xhtml");
    }

    public void publish() {
        if (addOrUpdate) {
            statute.setAuthor(LoginParam.getPer().getCpsn_number());
            statute.setCreatedate(new Date());
            statute.setLastchangedate(new Date());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String str = sdf.format(statute.getCreatedate());
            int id = (int) ((Integer) 100000 * Math.random());
            statute.setId(str + statute.getType() + id);
            statute.setAuditflag("0");
            if (imp) {
                statute.setImportance("1");
            } else {
                statute.setImportance("0");
            }
            System.out.println(statute.getTitle() + "---------" + statute.getOutline() + "|" + statute.getImportance() + "|" + statute.getContent() + "|" + statute.getType() + "|" + statute.getId());
            IbatisStatute.insertStatute(statute);
        } else {
            statute.setLastchangedate(new Date());
            IbatisStatute.updateStatute(statute);
        }
        statute = new Statute();
        NavigationTool.getSelectedUrl("/page/statute/choicetype.xhtml");
    }

    public void preview() {
        NavigationTool.getSelectedUrl("/page/statute/preview.xhtml");
    }

    public void back() {
        NavigationTool.getSelectedUrl("/page/statute/fckeditor.xhtml");
    }

    public void goback() {
        NavigationTool.getSelectedUrl("/page/statute/foreground.xhtml");
    }

    public void reselect() {
        NavigationTool.getSelectedUrl("/page/statute/choicetype.xhtml");
    }

    public void selected(TreeSelectionChangeEvent event) {//编辑-公告类型侦听
        
        type = (LawTypeData)  TreeUtil.listener(event);
        statute.setType(type.getId());
        FacesContext.getCurrentInstance().renderResponse();
    }

    public void selected2(TreeSelectionChangeEvent event) {//查看-公告类型侦听
        
        type = (LawTypeData)  TreeUtil.listener(event);
        String id = type.getId();
        statutelist = new ArrayList<Statute>();
        statutelist = IbatisStatute.getSelectStatute(id);
        FacesContext.getCurrentInstance().renderResponse();
    }

    public void showContent() {  //查看公告具体内容
        statute = (Statute) this.data.getRowData();
        NavigationTool.getSelectedUrl("/page/statute/showcontent.xhtml");
        FacesContext.getCurrentInstance().renderResponse();
    }

    public void showDetail(){
        statute = (Statute) this.data.getRowData();
        NavigationTool.getSelectedUrl("/law/person/showcontent.xhtml");
        FacesContext.getCurrentInstance().renderResponse();
    }

    public void backtolist() {
        NavigationTool.getSelectedUrl("/page/statute/statutelist.xhtml");
    }

    public void backCatalog() {
        NavigationTool.getSelectedUrl("/law/person/statutelist.xhtml");
    }

    public void isselect() {
        selectlist = statutelist;
        statutelist = new ArrayList<Statute>();
        if (selectlist != null && selectlist.size() > 0) {
            for (int i = 0; i < selectlist.size(); i++) {
                Statute statute = selectlist.get(i);
                statute.setCheck(all);
                statutelist.add(statute);
            }
        }

//        System.out.println("i am in isselec " + statutelist.size());
    }

    public void add() {
        addOrUpdate = true;
        statute = new Statute();
        NavigationTool.getSelectedUrl("/page/statute/choicetype.xhtml");
        FacesContext.getCurrentInstance().renderResponse();
    }

    public void update() {
        if (statute.getAuditflag().equals("1")) {
            
            ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "请先销审再修改");
        } else {
            addOrUpdate = false;
            NavigationTool.getSelectedUrl("/page/statute/fckeditorUpdate.xhtml");
            FacesContext.getCurrentInstance().renderResponse();
        }
    }

    public void delete() {
        if (statutelist != null && statutelist.size() > 0) {
            for (Statute ac : statutelist) {
                if (ac.isCheck()) {
                    if (ac.getAuditflag().equals("1")) {
                        
                        ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "未销审不能删除");
                    } else {
                        IbatisStatute.deleteSelectStatute(ac);
                    }
                }
            }
        }
        statutelist = IbatisStatute.getSelectStatute(type.getId());
    }

    public void auditflag() {
        if (statutelist != null && statutelist.size() > 0) {
            for (Statute ac : statutelist) {
                if (ac.isCheck()) {
                    ac.setAuditor(LoginParam.getPer().getCpsn_number());
                    ac.setAuditflag("1");
                    ac.setAuditdate(new Date());
                    IbatisStatute.updateSelectStatuteAudit(ac);
                }
            }
        }
    }

    public void cancelAuditflag() {
        if (statutelist != null && statutelist.size() > 0) {
            for (Statute ac : statutelist) {
                if (ac.isCheck()) {
                    ac.setAuditflag("0");
                    ac.setAuditor("");
                    IbatisStatute.cancelSelectStatuteAudit(ac);
                }
            }
        }
    }

    public void getmore() {
        leavinglist=new ArrayList();
        leavinglist = IbatisStatute.getLeavingStatute();
        NavigationTool.getSelectedUrl("/page/statute/getmore.xhtml");
    }

    public Statute getStatute() {
        return statute;
    }

    public void setStatute(Statute statute) {
        this.statute = statute;
    }

    public boolean isImp() {
        return imp;
    }

    public void setImp(boolean imp) {
        this.imp = imp;
    }

    public LawTypeData getType() {
        return type;
    }

    public void setType(LawTypeData type) {
        this.type = type;
    }

    public List<Statute> getStatutelist() {
        if (statutelist == null) {
            statutelist = new ArrayList<Statute>();
        }
        return statutelist;
    }

    public void setStatutelist(List<Statute> statutelist) {
        this.statutelist = statutelist;
    }

    public List<Statute> getSelectlist() {
        return selectlist;
    }

    public void setSelectlist(List<Statute> selectlist) {
        this.selectlist = selectlist;
    }

    public UIData getData() {
        return data;
    }

    public void setData(UIData data) {
        this.data = data;
    }

    public boolean isAll() {
        return all;
    }

    public void setAll(boolean all) {
        this.all = all;
    }

    public boolean isAddOrUpdate() {
        return addOrUpdate;
    }

    public void setAddOrUpdate(boolean addOrUpdate) {
        this.addOrUpdate = addOrUpdate;
    }

    public List<Statute> getTop3list() {
        top3list = IbatisStatute.getTop3Statute();
        return top3list;
    }

    public void setTop3list(List<Statute> top3list) {
        this.top3list = top3list;
    }

    public Statute getTopone() {
        topone = (Statute) IbatisStatute.getTop1Statute();
        return topone;
    }

    public void setTopone(Statute topone) {
        this.topone = topone;
    }

    public List<Statute> getLeavinglist() {

        return leavinglist;
    }

    public void setLeavinglist(List<Statute> leavinglist) {
        this.leavinglist = leavinglist;
    }

    public List<Statute> getTop5list() {
        top5list = IbatisStatute.getTop5Statute();
        return top5list;
    }

    public void setTop5list(List<Statute> top5list) {
        this.top5list = top5list;
    }
}
